
# -*- coding: utf-8 -*-


'''
拼多多任务领取

/*PHP是世界上最好的语言*/

'''

import requests, time,os
from base_config import  *
from goodscaseAPI import GoodsCase
from tagcaseAPI import CommentTag
from shopcaseAPI import ShopInfo
from commentcaseAPI import GoodsComment
from threading import Thread



'''
循环请求任务接口
'''

class RequestTask( object ):

    def __init__(self):

        self.sign = '67c067891a0425c63cc282478f336776'
        self.inum = 1

        pass

    # 请求任务
    def requestTask(self):

        data = {

            'sign':self.sign
        }

        url = domain_url['domain']['test_task_url']

        r = requests.post( url = url, data = data)

        # 正常领取任务
        if r.status_code == 200 and r.json()['code'] == 0:

            result = r.json()

            self.allotTask( result )




    # 分配任务
    def allotTask(self, params):

        data = params['data']


        # type = 1 关键词采集
        if int(data['type']) == 1:

            g = GoodsCase()

            keywords = {

                'taskid':data['id'],
                'keywords':data['keywords'],
                'page':data['page']
            }


            print('*-----------------------开始进行关键字详情任务采集---------------------------------*')
            g.parseJson( keywords )


        # 评价标签采集
        elif int( data['type'] ) == 3:

            t = CommentTag()

            tags = {
                'tasksid':data['id'],
                'goodsid':data['goodsid']
            }

            print('*-----------------------开始进行评价标签任务采集---------------------------------*')
            t.parsePage( tags )


        #评价SKU信息采集
        elif int(data['type']) == 4:

            c = GoodsComment()

            comments = {

                'tasksid':data['id'],
                'keywords':data['memo'],
                'page':data['page'],
                'goodsid':data['goodsid'],
                'startTime':data['startTime'],
                'endTime':data['endTime'],
            }
            print('*-----------------------开始进行商品SKU数据采集---------------------------------*')
            c.parseJson( comments )


        # 获取店铺信息
        elif int(data['type']) == 5:

            shop = {
                'tasksid':data['id'],
                'goodsid':data['goodsid']
            }

            s = ShopInfo()
            print('*-----------------------开始进行店铺信息数据采集---------------------------------*')
            s.parseJson( shop )

        #任务结束
        else:

            print('*------------------------任务全部完成睡眠60s再次发起请求--------------------------*')

            time.sleep(60)


    def main(self):

        while True:

            self.requestTask()

if __name__ == '__main__':

    # 获取线程数量
    import re

    max_threads = open(r'./config.ini',mode='r',encoding='utf-8').read()

    num = int(re.search(r'\d+',max_threads).group(0))

    r = RequestTask()

    # r.main()

    threads = []

    for i in range( num ):

        t1 = Thread( target=r.main,args=())

        t1.start()

        threads.append( t1 )

    for t in threads:

        t.join()